﻿
using Dapper;
using HBase;
using hCommand;
using Net.http;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;

namespace Persistence.abs
{
    public class AbsWhPkServer : AbsDoServer, IWhPkServer {


      

        public List<WhDopkDet> getDetials(long pid) {
            String sql = "select * from Wh_Dopk_Det  where pid=@pid";
            return readConn().Query<WhDopkDet>(sql, new { pid }).ToList();
        }
        public Page<WhDopk> queryPkPage(PkDto dto) {
            string sql = "select * from wh_dopk  where 1=1";

            if( dto.craneId > 0 ) {
                sql += " and  crane_id =@craneId";
            }
            if( dto.shelfId > 0 ) {
                sql += " and  shelf_Id =@shelfId";
            }
            if (dto.status > 0)
            {
                sql += " and  status =@status";
            }
            if( dto.itemId > 0 ) {
                sql += " and  item_Id =@itemId";  
            }
            if (dto.createBeginTime.Year>2000) {
                sql += " and  create_Time >@createBeginTime";
            }

            if (dto.createEndTime.Year > 2000) {
                
                dto.createEndTime= dto.createEndTime.AddDays(1);
                sql += " and  create_Time <@createEndTime";
            }
            string orderBy = "id desc";
            return this.queryPage<WhDopk>(sql ,orderBy ,dto);
        }
        public List<WhDopkRes> hisPage(PkDto dto) {
            string sql = "SELECT R.* FROM `wh_dopk_Res` R JOIN wh_dopk  P ON P.ID=R.PK_ID    where 1=1 ";
            if (dto != null) {
                if (dto.pkId > 0) {
                    sql += " and   pk_id = @pkId";
                } 
                if (dto.createBeginTime.Year > 2000) {
                    sql += " and  create_Time >@createBeginTime";
                } 
                if (dto.createEndTime.Year > 2000) { 
                    dto.createEndTime = dto.createEndTime.AddDays(1);
                    sql += " and  create_Time <@createEndTime";
                } 
            }
            
            return readConn().Query<WhDopkRes>(sql, dto).ToList();
        }


        public List<CoreStockDet> listPkOldDiffDetail(long pkId) { 
            string sql = "select *  FROM core_stock_det    where    doo_det_id=@pkId  or doi_det_id=@pkId"; 
            List<CoreStockDet> res = readConn().Query<CoreStockDet>(sql, new { pkId }).ToList();
            foreach(CoreStockDet det in res) {
                if (det.dooDetId == pkId) {
                    det.status = (int)StockDetailStatus.单据盘亏出库;
                } 
                if (det.doiDetId == pkId) {
                    det.status = (int)StockDetailStatus.盘盈;
                } 
            }
            return res;
        }
        public int countPkingStock(long pkId) {
            string sql = "select count(*) from core_stock s join core_task t on  s.id=t.stock_id  where PK_ID=@pkId  and t.status<>" + ( int )CoreTaskStatus.结束+ " and t.status<>" + ( int )CoreTaskStatus.删除;

            return readConn().QueryUnique<int>(sql ,new { pkId });
        }
        public int finshPk(long pkId) {


          
            return 0;

        }
        public void updateCSD(long id, double count)
        {
            string sql = "update core_stock_det  set count_db=@count  where id = @id  ";

            opConn().Execute(sql, new { id, count });
           
        }
        public void selectStock(long pkId, string add, string del) {
            string sql = "update core_stock  set pk_id=@pkId  where id in @id  ";

            opConn().Execute(sql, new { pkId,id=add.Split(",") });
            opConn().Execute(sql, new { pkId=0, id = del.Split(",") });
        }
    }
}
